Page({

    /**
     * 页面的初始数据
     */
    
    data: {
        nickname: '点击登录',
        avatarUrl: '../../imgs/login.png',
        code: '',
        qrCodeUrl: '../../imgs/qrCode.svg',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        wx.login({
            success: function(login_res) {
            that.setData({
                code:login_res.code
            })
            wx.setStorageSync('code', login_res.code)
            }
        })
        if (wx.getStorageSync('userinfo') != '') {
          let userinfo = wx.getStorageSync('userinfo')
            that.setData({
              nickname: userinfo.nickName,
              avatarUrl: userinfo.avatarUrl,
              qrCodeUrl: wx.getStorageSync('qrCodeUrl')
            })
        }
    },

    checkLogin:function() {
      let that=this
        if (wx.getStorageSync('userid') == '') {
          // wx.showToast({
          //   title: '请先登录',
          //   icon:'none'
          // })
          wx.showModal({
            cancelColor: 'cancelColor',
            title: '您还未登录',
            content: '请先登录再进行操作',
            confirmText:'立即登录',
            cancelText:'暂不登录',
            success:function(res){
              if(res.confirm){
                that.userLogin()
              }
            }
          })
          return false
        }
        return true
    },
    userLogin : function() {
        if (wx.getStorageSync('userid') == '') {
            let that = this
            wx.getUserProfile({
                desc:'请求用户授权',
                success: function(info_res) {
                  console.log(info_res)
                  let userinfo = info_res.userInfo
                  wx.setStorageSync('userinfo', userinfo)
                  that.setData({
                      nickname: userinfo.nickName,
                      avatarUrl: userinfo.avatarUrl
                  })
                  // 2. 小程序通过wx.request()发送code到开发者服务器
                  wx.request({
                     
                //  url: 'https://www.intohash.com/vote_backend/user/login',
                 url: 'http://localhost:8010/vote_backend/user/login',
                    method: 'POST',
                    data: {
                      authTyoe: 1 ,
                      code: wx.getStorageSync('code'), //临时登录凭证
                      encrypteData: info_res.encryptedData, //用户敏感信息
                      iv: info_res.iv, //解密算法的向量
                      rawData: info_res.rawData, //用户非敏感信息
                      signature: info_res.signature //签名   
                    },
                    success: function(res) {
                      console.log(res);
                      console.log(res.data.data.openid);
                     
                      console.log('验证成功')
                      wx.setStorageSync('skey', res.data.data.sKey)
                      wx.setStorageSync('userid', res.data.data.openid)
                      wx.request({   
                        
                   // url: 'https://www.intohash.com/vote_backend/user/qrCode/'+res.data.data.openid,
                       
                    url: 'http://localhost:8010/vote_backend/user/qrCode/'+res.data.data.openid,
                        method: "GET",
                        success:function(res) {
                          console.log(res);
                          wx.setStorageSync('qrCodeUrl', res.data.data)
                          that.setData({
                            qrCodeUrl: res.data.data
                          })
                        }
                      })
                    },
                    fail: function(error) {
                      //调用服务端登录接口失败
                      wx.showToast({
                        title: '登陆失败!',
                        duration: 2000,
                        icon: 'none' 
                      })
                      console.log(error);
                    }
                  })
                }
              })
        }
    },
    previewImage() {
      if (wx.getStorageSync('userid') != '') {
        wx.navigateTo({
         url: '/pages/showMyQRCode/showMyQRCode?myQRCodeImgURL=' + this.data.qrCodeUrl,
       })

      }
    },
    onTapCreateVote() {
        if (this.checkLogin() == true) {
          wx.navigateTo({
            url: '/pages/createVote/createVote'
          })
        }
      },
      onTapCreateScore() {
        if (this.checkLogin() == true) {
          wx.navigateTo({
            url: '/pages/createScore/createScore'
          })
        }
      },
      onTapCreateVoteBox() {
        if (this.checkLogin() == true) {
          // wx.navigateTo({
          //   url: '/pages/createVoteBox/createVoteBox'
          // })
        }
      },
      onTapCreateImgVote() {
        if (this.checkLogin() == true) {
          wx.navigateTo({
            url: '/pages/createImgVote/createImgVote'
          })
        }
      },
      onTapMyCreatedVote() {
        if (this.checkLogin() == true) {
          wx.navigateTo({
            url: '/pages/myCreatedVote/myCreatedVote'
          })
        }
      },
      onTapMyJoinedVote() {
        if (this.checkLogin() == true) {
          wx.navigateTo({
            url: '/pages/myJoinedVote/myJoinedVote'
          })
        }
      }
   
    
})